﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BOM.entity;
using System.Data;
using System.Data.OleDb;
using BOM.BaseData;

namespace BOM.dao
{
    class CustomerNew
    {
        //查询输入的编号在数据库中是否有值
        public int CustomerChaXun()
        {
            Customer0 customer = new Customer0();
            string cmdText = "select fnumber from customer where fnumber = '" + customer.getFnumber() + "'";
            DataTable table=new DataTable();
            //运用try --catch 判断表中是否有值
            try
            {
                table = DbService.getSQLTable(cmdText, "customer");
                DataRow row = table.Rows[0]; //这个能执行 说明编号能查出数据 该修改 否则 不能查出数据 该新增
                return 1;
            }
            catch
            {
                return 0;
            }
        }
        //向表中新增数据
        public int CustomerAdd()
        {
            Customer0 customer = new Customer0();
            string cmdText;
            string Str = "'" + customer.getFnumber() + "','" + customer.getFname() + "','" + customer.getFlinkman() + "','" + customer.getFaddress() + "','" + customer.getFobile() + "','";
            Str += "" + customer.getFtel() + "','" + customer.getFemail() + "','" + customer.getFqq() + "'";
            cmdText = "insert into customer (fnumber,fname,flinkman,faddress,fmobile,ftel,femail,fqq) values (" + Str + ")";
            int valus = 0;
            valus = DbService.executeSQL(cmdText);
            if (valus <= 0)
                return 0;
            else
                return 1; //新增成功返回 1
        }
        //修改表中数据
        public int CustomerXiuGai()
        {
            Customer0 customer = new Customer0();
            string str = customer.getFnumber();
            string cmdText = "delete from customer where fnumber = '" + str + "'";
            int valus = 0;
            valus = DbService.executeSQL(cmdText);
            if (valus > 0)
            {
                int i = CustomerAdd();
                if (i > 0)
                {
                    return 1;
                }
                else
                    return 0;
            }
            else
                return 0;
        }
        //删除表中数据
        public int CustomerShanChu()
        {
            Customer0 customer = new Customer0();
            //获取编号 根据编号删除数据
            string str = customer.getFnumber();
            string cmdText = "delete from customer where fnumber = '" + str + "'";
            int valus = 0;
            valus = DbService.executeSQL(cmdText);
            if (valus == 1)
                return 1;
            else
                return 0;
        }
        //根据ID取customer表中的数据
        public int CustomerChak(int fid)
        {
            DataTable table = new DataTable();
            string cmdText = "select * from customer where fID = " + fid + "";
            table = DbService.getSQLTable(cmdText, "cutomer");
            try
            {
                DataRow row = table.Rows[0];
                Customer0 customer = new Customer0();
                customer.setFnumber(row["fnumber"].ToString());
                customer.setFname(row["fname"].ToString());
                customer.setFlinkman(row["flinkman"].ToString());
                customer.setFobile(row["fmobile"].ToString());
                customer.setFaddress(row["faddress"].ToString());
                customer.setFemail(row["femail"].ToString());
                customer.setFqq(row["fqq"].ToString());
                customer.setFtel(row["ftel"].ToString());
                return 1;
            }
            catch
            {
                return 0;
            }
        }
        //根据ID删除表中数据
        public int CustomerListShanChu(int id)
        {
            string cmdText = "delete from customer where fid = " + id + "";
            int valus = 0;
            valus = DbService.executeSQL(cmdText);
            if (valus == 1)
                return 1;
            else
                return 0;
        }
        //查询Customer表中所有数据
        public DataTable CustomerListChaKan()
        {
            string cmdText = "select * from customer";
            DataTable table = DbService.getSQLTable(cmdText, "customer");
            return table;
        }
        //根据编号查询 得到一个表
        public static DataTable CustomerNumberSelect(string fname)
        {
            string cmdText = "select * from customer where fname = '" + fname + "'";
            DataTable table = DbService.getSQLTable(cmdText, "customer");
            if (table.Rows.Count == 1)
            {
                return table;
            }
            else
            {
                return null;
            }
        }


    }
}
